﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Configuration;
/***************************
 * 公共类
 * 
 * Author：JXL
 ***************************/ 
namespace HouseCommon
{
    public class Common
    {
        /// <summary>
        /// 连接字符串
        /// </summary>
        public static readonly string conn = ConfigurationManager.ConnectionStrings["conn"].ToString().Trim();
        /// <summary>
        /// 反射的名称
        /// </summary>
        //public static readonly string _assemblyName = ConfigurationManager.AppSettings["House"].ToString().Trim();

        public static string sqltostring<T>(T t, string tablename, Dictionary<string, bool> pkname) where T : class
        {
            string sqlstr = "insert into {0}({1}) values({2})";
            string filde = "";
            string value = "";
            foreach (var item in t.GetType().GetProperties())
            {
                if (pkname.Values.First() && item.Name == pkname.Keys.First())
                {

                    continue;
                }
                filde += item.Name + ",";
                if (item.PropertyType == typeof(string) || item.PropertyType == typeof(DateTime) || item.PropertyType == typeof(DateTime?))
                {
                    value += "'" + item.GetValue(t) + "'" + ",";
                }
                else
                {
                    value += item.GetValue(t) + ",";
                }
            }
            filde = filde.TrimEnd(',');
            value = value.TrimEnd(',');
            return string.Format(sqlstr, tablename, filde, value);
        }
        /// <summary>
        /// 
        /// 修改
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="t"></param>
        /// <param name="w"></param>
        /// <param name="tablename"></param>
        /// <param name="pkname"></param>
        /// <returns></returns>
        public static string updatetostring<T>(T t, string w, string tablename, Dictionary<string, bool> pkname)
        {

            string sqlstr = "update {0} set {1} where {2} in ({3})";
            var filde = "";

            foreach (var item in t.GetType().GetProperties())
            {
                if (pkname.Values.First() && item.Name == pkname.Keys.First())
                {
                    continue;
                }
                if (item.PropertyType == typeof(string) || item.PropertyType == typeof(DateTime) || item.PropertyType == typeof(DateTime?))
                {
                    filde += item.Name + "='" + item.GetValue(t) + "',";
                }
                else
                {
                    filde += item.Name + "=" + item.GetValue(t) + ",";
                }
            }
            filde = filde.TrimEnd(',');
            return string.Format(sqlstr, tablename, filde, pkname.Keys.First(), w);
        }
        /// <summary>
        /// 修改根据项目号
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="t"></param>
        /// <param name="w"></param>
        /// <param name="tablename"></param>
        /// <param name="pkname"></param>
        /// <returns></returns>
        public static string updatetostrings<T>(T t, string w, string tablename, Dictionary<string, bool> pkname)
        {

            string sqlstr = "update {0} set {1} where {2} ";
            var filde = "";

            foreach (var item in t.GetType().GetProperties())
            {
                if (pkname.Values.First() && item.Name == pkname.Keys.First())
                {
                    continue;
                }
                if (item.PropertyType == typeof(string) || item.PropertyType == typeof(DateTime) || item.PropertyType == typeof(DateTime?))
                {
                    filde += item.Name + "='" + item.GetValue(t) + "',";
                }
                else
                {
                    filde += item.Name + "=" + item.GetValue(t) + ",";
                }
            }
            filde = filde.TrimEnd(',');
            return string.Format(sqlstr, tablename, filde,  w);
        }
        /// <summary>
        /// 查询
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="where"></param>
        /// <param name="tablename"></param>
        /// <returns></returns>
        public static string gettostring<T>(string where, string tablename) where T : new()
        {
            T t = new T();
            string sqlstr = "select {0} from {1} where {2}";
            
            string filde = "";
            foreach (var item in t.GetType().GetProperties())
            {
                filde += item.Name + ",";
            }
            filde = filde.TrimEnd(',');
            return string.Format(sqlstr, filde, tablename, where);

        }

        
    }
}
